﻿using Elderlyhealth.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserActivityLog : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadPendingRequests();
            LoadUserActivityLog();
            LoadAllLogs(); // 加载并显示所有日志
        }
    }
    private void LoadAllLogs()
    {
        using (var db = new DataClasses1DataContext())
        {
            // 获取所有日志数据
            var logs = from log in db.TUserActivityLog
                       orderby log.Timestamp descending
                       select log;

            gvUserActivityLog.DataSource = logs.ToList();
            gvUserActivityLog.DataBind();
        }
    }
    protected void filterRecords(object sender, EventArgs e)
    {
        int userId;
        bool userIdValid = int.TryParse(txtUserId.Text.Trim(), out userId);
        string action = txtAction.Text.Trim();

        using (var db = new DataClasses1DataContext())
        {
            // 基本查询，获取所有日志
            var query = from log in db.TUserActivityLog
                        select log;

            // 如果用户ID有效，添加用户ID筛选条件
            if (userIdValid)
            {
                query = query.Where(log => log.UserId == userId);
            }

            // 如果操作内容不为空，添加操作内容筛选条件
            if (!string.IsNullOrEmpty(action))
            {
                query = query.Where(log => log.Action.Contains(action));
            }

            // 绑定查询结果到GridView
            gvUserActivityLog.DataSource = query.OrderByDescending(log => log.Timestamp).ToList();
            gvUserActivityLog.DataBind();
        }
    }
    private void LoadUserActivityLog()
    {
        using (var db = new DataClasses1DataContext())
        {
            var logs = (from log in db.TUserActivityLog
                        orderby log.Timestamp descending
                        select new
                        {
                            log.LogId,
                            log.UserId,
                            log.UserType,
                            log.Action,
                            log.Timestamp,
                            log.IPAddress
                        }).ToList();

            gvUserActivityLog.DataSource = logs;
            gvUserActivityLog.DataBind();
        }
    }
    // 加载待审核的绑定请求
    private void LoadPendingRequests()
    {
        using (var db = new DataClasses1DataContext())
        {
            var pendingRequests = db.RelativeOldMapping
                .Where(r => r.审核状态 == false)
                .Select(r => new
                {
                    r.子女id,
                    r.老人id,
                    r.绑定时间
                }).ToList();

            gvRequests.DataSource = pendingRequests;
            gvRequests.DataBind();
        }
    }
    protected void btnApprove_Click(object sender, EventArgs e)
    {
        using (var db = new DataClasses1DataContext())
        {
            foreach (GridViewRow row in gvRequests.Rows)
            {
                CheckBox cbSelect = (CheckBox)row.FindControl("cbSelect");
                if (cbSelect != null && cbSelect.Checked)
                {
                    int relativeId = Convert.ToInt32(gvRequests.DataKeys[row.RowIndex]["子女id"]);
                    int oldId = Convert.ToInt32(gvRequests.DataKeys[row.RowIndex]["老人id"]);

                    var mapping = db.RelativeOldMapping
                        .SingleOrDefault(m => m.子女id == relativeId && m.老人id == oldId && m.审核状态 == false);

                    if (mapping != null)
                    {
                        mapping.审核状态 = true;
                    }
                }
            }

            db.SubmitChanges();
            lblMessage.Text = "选中的请求已审核通过。";
            LoadPendingRequests(); // 重新加载数据
        }
    }

    protected void cbSelectAll_CheckedChanged1(object sender, EventArgs e)
    {
        CheckBox cbSelectAll = (CheckBox)sender;
        foreach (GridViewRow row in gvRequests.Rows)
        {
            CheckBox cbSelect = (CheckBox)row.FindControl("cbSelect");
            cbSelect.Checked = cbSelectAll.Checked;
        }
    }

    protected void cbSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cbSelectAll = (CheckBox)sender;
        foreach (GridViewRow row in gvRequests.Rows)
        {
            CheckBox cbSelect = (CheckBox)row.FindControl("cbSelect");
            cbSelect.Checked = cbSelectAll.Checked;
        }
    }


    /*protected void btnFilter_Click(object sender, EventArgs e)
    {
        int userId;
        string action = txtAction.Text.Trim();

        using (var db = new DataClasses1DataContext())
        {
            var query = db.TUserActivityLog.AsQueryable();

            if (int.TryParse(txtUserId.Text.Trim(), out userId))
            {
                query = query.Where(log => log.UserId == userId);
            }

            if (!string.IsNullOrEmpty(action))
            {
                query = query.Where(log => log.Action.Contains(action));
            }

            var logs = query.OrderByDescending(log => log.Timestamp).ToList();

            gvUserActivityLog.DataSource = logs;
            gvUserActivityLog.DataBind();
        }
    }*/

    protected void btnFilter_Click1(object sender, EventArgs e)
    {
        int userId;
        string action = txtAction.Text.Trim();

        using (var db = new DataClasses1DataContext())
        {
            var query = db.TUserActivityLog.AsQueryable();

            if (int.TryParse(txtUserId.Text.Trim(), out userId))
            {
                query = query.Where(log => log.UserId == userId);
            }

            if (!string.IsNullOrEmpty(action))
            {
                query = query.Where(log => log.Action.Contains(action));
            }

            var logs = query.OrderByDescending(log => log.Timestamp).ToList();

            gvUserActivityLog.DataSource = logs;
            gvUserActivityLog.DataBind();
        }
    }

    protected void btnShowAll_Click(object sender, EventArgs e)
    {
        using (var db = new DataClasses1DataContext())
        {
            var allLogs = from log in db.TUserActivityLog
                          orderby log.Timestamp descending
                          select log;

            gvUserActivityLog.DataSource = allLogs.ToList();
            gvUserActivityLog.DataBind();
        }
    }
}
